<?php
	session_start();
?>
<html>
	<head>
		<title>Login</title>
		<link rel="stylesheet" type="text/css" href="styleBackend.css"/>
		<link rel="stylesheet" type="text/css" href="styleLogin.css"/>
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
		<script>
			$(document).ready(function(){
				$("#settingsButton").click(function(){
					$("#settingsBox").fadeToggle();
				});
				
				$("#changePW").submit(function(e){
					
					var ok = true;
					$(this).find("input[type=password]").each(function(){
						if($(this).val() == ""){
							$(this).css("border-color", "red");
							ok = false;
						}
						else
							$(this).css("border-color", "");
					});
					return ok;
				});
				
				$("#changePW").find("input[type=reset]").click(function(){
					$("#changePW").find("input[type=password]").each(function(){
							$(this).css("border-color", "");
					});
				});
				
				$("#login form").submit(function(e){
					var ok = true;
					$(this).find("input[type=password]").each(function(){
						if($(this).val() == ""){
							$(this).css("border-color", "red");
							ok = false;
							$("<div class='errorBox'>Passwort eingeben!</div>").appendTo("body").delay(1500).fadeOut(1000, 
										function(){
													$("#login form input[type=password]").css("border-color", ""); 
													$(".infoBox").remove();
												  });
						}
					});
					return ok;
				});
				
				$("body").find(".infoBox").delay(2500).fadeOut(1000, function(){$(".infoBox").remove(); });
				$("body").find(".errorBox").delay(2500).fadeOut(1000, function(){$(".errorBox").remove(); });
				
			});
			

			
		</script>
	</head>
	<body>
		
		<?php
		
		include("funktionenLogin.php");
		
		if(isset($_GET['create'])){ 
			echo "<div class='infoBox'>";
			if( erzeuge_datenbank_tabellen() !== false ) 
				echo "DB-Tabellen erfolgreich angelegt.<br>";
			echo "</div>";				
		}	
		
		if(isset($_POST['changeEmail'])){ 
			if( emailAdresse($_POST['changeEmail']) !== false ) 
				echo "<div class='infoBox'>E-Mail erfolgreich gewechselt.<br>";
			else
				echo "<div class='errorBox'>Neue Email konnte nicht gesetzt werden!<br>";
			echo "</div>";
		}
		
		if(isset($_POST['logout'])){ 
			echo "<div class='infoBox'>... ausgeloggt!</div>";
			$_SESSION['login'] = 0;
			session_destroy();		
		}	
		
		if( isset($_POST['pw']) !== false ){ // PASSWORT ANFRAGE
			if( checkPasswort($_POST['pw']) == "1" ){
				if(isset($_POST['pwNeu'])){  
					if( setMD5Password($_POST['pwNeu']) !== false ) 
						echo "<div class='infoBox'>Passwort-Wechsel erfolgreich.<br>";
					else
						echo "<div class='errorBox'>Neues Passwort konnte nicht gesetzt werden!<br>";
					echo "</div>";
				}
				$_SESSION['login'] = 1;
				showBackend();
			}
			else{
				$_SESSION['login'] = 0;
				session_destroy();
				echo "<div class='errorBox'>Passwort falsch!</div>";
				showLoginForm();
			}
		}	
		else if( isset($_SESSION['login']) !== false ){ // EINGELOGGT STATUS
			if($_SESSION['login'] == "0"){showLoginForm(); return;}
			showBackend();
		}
		else{ // NICHT EINGELOGGT
			showLoginForm();
		}
		?>
	</body>
</html>